package com.zhu.component;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class CacheTemplate {

    Map<Object,Object> cache = new ConcurrentHashMap<>();

    public void set(Object key, Object val) {
        if (!StringUtils.isEmpty(key)){
            cache.put(key, val);
        } else {
            throw new RuntimeException("key 不能为空!");
        }
    }

    public Object get(Object key) {
        if (!StringUtils.isEmpty(key)){
            return cache.get(key);
        } else {
            throw new RuntimeException("key 不能为空!");
        }
    }

    public Object get(Object key, Object defVal) {
        if (!StringUtils.isEmpty(key)){
            return cache.getOrDefault(key, defVal);
        } else {
            throw new RuntimeException("key 不能为空!");
        }
    }
}
